#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define endl '\n'
const int maxn = 100 + 50;
const int maxm = maxn * maxn;
const int INF = 1e8;

int dis[maxn][maxn], dp[maxn][maxn];
int book[maxn];//book=0表示尚未扩展到的点
int n, m;


void Prim() {
    memset(book, 0, sizeof(book));
    book[1] = 1;//先把{1} 加入已扩展集合
    int tot = 0;
    while (1) {
        for (int i = 1; i <= n; i++) {
            if (book[i] == 1)
                cerr << i << " ";

        }
        cerr << "|";
        for (int i = 1; i <= n; i++) {
            if (book[i] == 0)
                cerr << i << " ";

        }
        cerr << endl;
        int w = INT_MAX, x = -1, y = -1;
        for (int i = 1; i <= n; i++) {
            if (book[i] == 0)continue;
            for (int j = 2; j <= n; j++) {
                if (book[j] == 1 || dp[i][j])continue;
                if (dis[i][j] < w) {
                    x = i;
                    y = j;
                    w = dis[i][j];
                }
            }
        }
        if (y == -1)break;
        book[y] = 1;
        tot += w;
        cerr << x << "-(" << dis[x][y] << ")->" << y << endl;
        dp[x][y] = 1;

    }

    cout << tot << endl;

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ifstream fin("graph.in");
    if (fin.good()) cin.rdbuf(fin.rdbuf());

    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            if (i == j)dis[i][j] = 0;
            else dis[i][j] = INF;
        }
    while (m--) {
        int x, y, w;
        cin >> x >> y >> w;
        dis[x][y] = dis[y][x] = w;
    }


    Prim();

    return 0;
}